```html
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Kafka架构深度解析 | 技术小馆</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            color: #333;
            line-height: 1.8;
        }
        h1, h2, h3, h4, h5, h6 {
            font-family: 'Noto Serif SC', serif;
            font-weight: 600;
        }
        .hero-overlay {
            background: linear-gradient(to right, rgba(0,0,0,0.8) 0%, rgba(0,0,0,0.2) 100%);
        }
        .code-block {
            background-color: #2d3748;
            color: #f8fafc;
            border-left: 4px solid #4a5568;
        }
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);
        }
        .highlight {
            position: relative;
        }
        .highlight::after {
            content: '';
            position: absolute;
            bottom: 0;
            left: 0;
            width: 100%;
            height: 10px;
            background-color: rgba(167, 139, 250, 0.2);
            z-index: -1;
            transform: scaleX(0.9);
            transition: all 0.3s ease;
        }
        .highlight:hover::after {
            height: 15px;
            transform: scaleX(1);
            background-color: rgba(167, 139, 250, 0.3);
        }
    </style>
</head>
<body class="bg-gray-50">
    <!-- Hero Section -->
    <section class="relative bg-gray-900 text-white">
        <div class="absolute inset-0 bg-cover bg-center z-0 hero-overlay" style="background-image: url('https://images.unsplash.com/photo-1620712943543-bcc4688e7485?ixlib=rb-1.2.1&auto=format&fit=crop&w=1350&q=80');"></div>
        <div class="container mx-auto px-6 py-32 relative z-10">
            <div class="max-w-3xl">
                <h1 class="text-4xl md:text-5xl font-bold mb-6 leading-tight">Kafka架构深度解析</h1>
                <p class="text-xl md:text-2xl text-gray-300 mb-8">解密高吞吐量分布式消息系统的设计精髓</p>
                <div class="flex items-center">
                    <div class="w-12 h-12 rounded-full bg-purple-500 flex items-center justify-center mr-4">
                        <i class="fas fa-rocket text-white text-xl"></i>
                    </div>
                    <div>
                        <p class="text-gray-300 font-medium">技术小馆出品</p>
                        <p class="text-gray-400 text-sm">深入探讨分布式系统核心技术</p>
                    </div>
                </div>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <main class="container mx-auto px-6 py-16">
        <div class="max-w-4xl mx-auto">
            <!-- Author & Meta -->
            <div class="flex items-center mb-12">
                <div class="flex-shrink-0 mr-4">
                    <div class="w-16 h-16 rounded-full bg-gradient-to-r from-purple-500 to-blue-500 flex items-center justify-center text-white text-2xl">
                        <i class="fas fa-user-tie"></i>
                    </div>
                </div>
                <div>
                    <h3 class="font-bold text-gray-800">技术小馆专家团队</h3>
                    <div class="flex text-gray-500 text-sm mt-1">
                        <span class="mr-4"><i class="fas fa-calendar-alt mr-1"></i> 2023年8月更新</span>
                        <span><i class="fas fa-clock mr-1"></i> 约15分钟阅读</span>
                    </div>
                </div>
            </div>

            <!-- Introduction -->
            <div class="mb-16 bg-gradient-to-r from-purple-50 to-blue-50 p-8 rounded-xl border border-purple-100">
                <p class="text-lg text-gray-700 mb-4">
                    <span class="text-4xl float-left font-serif mr-4 -mt-2 text-purple-600">K</span>afka 是一个高吞吐量的分布式消息系统，其架构设计非常精妙，能够支持大规模的消息传输和高效的数据持久化。本文将深入剖析Kafka的核心设计理念、工作机制和实现细节，帮助您全面理解这个强大的分布式消息系统。
                </p>
            </div>

            <!-- Architecture Overview -->
            <section class="mb-20">
                <div class="flex items-center mb-8">
                    <h2 class="text-3xl font-bold text-gray-800 highlight inline-block">1. Kafka 架构概述</h2>
                    <div class="ml-4 w-12 h-12 rounded-full bg-blue-100 flex items-center justify-center">
                        <i class="fas fa-sitemap text-blue-600 text-xl"></i>
                    </div>
                </div>
                <p class="text-gray-700 mb-6">Kafka 的架构基于 <strong>分布式日志</strong>，由多个组件协同工作，共同实现高效的消息传递、持久化和消费。Kafka 的核心组件包括：</p>
                
                <div class="grid md:grid-cols-2 gap-6 mb-8">
                    <div class="bg-white p-6 rounded-lg shadow-md border-t-4 border-blue-500 card-hover transition-all duration-300">
                        <div class="flex items-center mb-4">
                            <div class="w-10 h-10 rounded-full bg-blue-100 flex items-center justify-center mr-4">
                                <i class="fas fa-paper-plane text-blue-600"></i>
                            </div>
                            <h3 class="text-xl font-semibold">Producer</h3>
                        </div>
                        <p class="text-gray-700">消息生产者，负责将消息发送到 Kafka 集群。</p>
                    </div>
                    <div class="bg-white p-6 rounded-lg shadow-md border-t-4 border-purple-500 card-hover transition-all duration-300">
                        <div class="flex items-center mb-4">
                            <div class="w-10 h-10 rounded-full bg-purple-100 flex items-center justify-center mr-4">
                                <i class="fas fa-server text-purple-600"></i>
                            </div>
                            <h3 class="text-xl font-semibold">Broker</h3>
                        </div>
                        <p class="text-gray-700">消息中介，Kafka 集群中的每个节点。</p>
                    </div>
                    <div class="bg-white p-6 rounded-lg shadow-md border-t-4 border-green-500 card-hover transition-all duration-300">
                        <div class="flex items-center mb-4">
                            <div class="w-10 h-10 rounded-full bg-green-100 flex items-center justify-center mr-4">
                                <i class="fas fa-users text-green-600"></i>
                            </div>
                            <h3 class="text-xl font-semibold">Consumer</h3>
                        </div>
                        <p class="text-gray-700">消息消费者，消费 Kafka 中的消息。</p>
                    </div>
                    <div class="bg-white p-6 rounded-lg shadow-md border-t-4 border-yellow-500 card-hover transition-all duration-300">
                        <div class="flex items-center mb-4">
                            <div class="w-10 h-10 rounded-full bg-yellow-100 flex items-center justify-center mr-4">
                                <i class="fas fa-cogs text-yellow-600"></i>
                            </div>
                            <h3 class="text-xl font-semibold">Zookeeper</h3>
                        </div>
                        <p class="text-gray-700">协调者，管理 Kafka 的元数据和集群状态。</p>
                    </div>
                </div>

                <div class="bg-blue-50 p-6 rounded-lg border-l-4 border-blue-500 mb-8">
                    <p class="text-blue-800 mb-4"><i class="fas fa-lightbulb text-blue-500 mr-2"></i> Kafka 的架构设计中，核心思想是通过分区来水平扩展并提高并发能力，同时利用副本保证数据的高可用性。</p>
                </div>

                <div class="mermaid bg-white p-6 rounded-lg shadow-md">
                    graph TD
                        A[Producer] -->|发布消息| B[Broker集群]
                        B -->|存储消息| C[(Topic/Partition)]
                        C -->|消费消息| D[Consumer Group]
                        E[Zookeeper] -->|协调集群| B
                        E -->|管理元数据| D
                        style A fill:#9F7AEA,color:white
                        style B fill:#4299E1,color:white
                        style C fill:#38B2AC,color:white
                        style D fill:#667EEA,color:white
                        style E fill:#F6AD55,color:white
                </div>
            </section>

            <!-- Topic and Partition -->
            <section class="mb-20">
                <div class="flex items-center mb-8">
                    <h2 class="text-3xl font-bold text-gray-800 highlight inline-block">2. Topic 和 Partition 的设计</h2>
                    <div class="ml-4 w-12 h-12 rounded-full bg-purple-100 flex items-center justify-center">
                        <i class="fas fa-project-diagram text-purple-600 text-xl"></i>
                    </div>
                </div>
                <p class="text-gray-700 mb-6">Kafka 使用 <strong>Topic</strong> 来标识消息的分类，每个 <strong>Topic</strong> 被划分为多个 <strong>Partition</strong>。这使得 Kafka 可以通过水平扩展的方式将负载分担到多个 Broker 节点。每个 <strong>Partition</strong> 是一个 <strong>日志文件</strong>，Kafka 将消息按照顺序写入文件。</p>
                
                <div class="bg-gray-800 text-gray-100 p-4 rounded-lg code-block mb-6">
                    <div class="flex items-center text-sm mb-4 text-gray-400">
                        <div class="w-3 h-3 rounded-full bg-red-500 mr-2"></div>
                        <div class="w-3 h-3 rounded-full bg-yellow-500 mr-2"></div>
                        <div class="w-3 h-3 rounded-full bg-green-500 mr-2"></div>
                        <span class="ml-2">TopicPartition.java</span>
                    </div>
                    <pre><code class="language-java">// 创建新的 Partition
public class TopicPartition {
    private final String topic;
    private final int partitionId;
    
    public TopicPartition(String topic, int partitionId) {
        this.topic = topic;
        this.partitionId = partitionId;
    }
}</code></pre>
                </div>

                <p class="text-gray-700 mb-6">每个分区内部的消息有一个唯一的 <strong>Offset</strong>，消费者可以根据这个 Offset 来读取消息。通过对每个 Partition 的分布式管理，Kafka 能够高效地处理大规模并发请求。</p>

                <div class="bg-white p-6 rounded-lg shadow-md border border-gray-200">
                    <div class="flex items-center mb-4">
                        <i class="fas fa-info-circle text-blue-500 mr-2"></i>
                        <h3 class="text-lg font-semibold">Partition设计优势</h3>
                    </div>
                    <ul class="space-y-3 text-gray-700">
                        <li class="flex items-start">
                            <div class="flex-shrink-0 h-6 w-6 rounded-full bg-blue-100 flex items-center justify-center mr-3">
                                <i class="fas fa-bolt text-blue-500 text-xs"></i>
                            </div>
                            <span>水平扩展：通过增加Partition数量扩展处理能力</span>
                        </li>
                        <li class="flex items-start">
                            <div class="flex-shrink-0 h-6 w-6 rounded-full bg-blue-100 flex items-center justify-center mr-3">
                                <i class="fas fa-random text-blue-500 text-xs"></i>
                            </div>
                            <span>并行处理：不同Partition可以被不同Consumer并行处理</span>
                        </li>
                        <li class="flex items-start">
                            <div class="flex-shrink-0 h-6 w-6 rounded-full bg-blue-100 flex items-center justify-center mr-3">
                                <i class="fas fa-shield-alt text-blue-500 text-xs"></i>
                            </div>
                            <span>容错能力：每个Partition可以配置多个副本保证数据安全</span>
                        </li>
                    </ul>
                </div>
            </section>

            <!-- Message Storage -->
            <section class="mb-20">
                <div class="flex items-center mb-8">
                    <h2 class="text-3xl font-bold text-gray-800 highlight inline-block">3. 消息写入与存储：日志结构</h2>
                    <div class="ml-4 w-12 h-12 rounded-full bg-green-100 flex items-center justify-center">
                        <i class="fas fa-database text-green-600 text-xl"></i>
                    </div>
                </div>
                <p class="text-gray-700 mb-6">Kafka 的消息存储采用 <strong>顺序写入日志</strong> 的方式。这使得 Kafka 在写入消息时能够高效利用磁盘的顺序访问特性。每个 Partition 的消息被存储为一个日志文件，文件以 <strong>Segment</strong> 形式存在，Kafka 将消息追加到 Segment 文件的末尾。</p>
                
                <div class="bg-gray-800 text-gray-100 p-4 rounded-lg code-block mb-6">
                    <div class="flex items-center text-sm mb-4 text-gray-400">
                        <div class="w-3 h-3 rounded-full bg-red-500 mr-2"></div>
                        <div class="w-3 h-3 rounded-full bg-yellow-500 mr-2"></div>
                        <div class="w-3 h-3 rounded-full bg-green-500 mr-2"></div>
                        <span class="ml-2">LogSegment.java</span>
                    </div>
                    <pre><code class="language-java">// 写入日志的操作，使用顺序写入提高性能
MappedByteBuffer buffer = segmentFileChannel.map(FileChannel.MapMode.READ_WRITE, position, size);
buffer.put(messageBytes);</code></pre>
                </div>

                <p class="text-gray-700 mb-6">日志文件的大小是可配置的，当 Segment 文件达到最大尺寸时，Kafka 会切换到下一个日志文件。通过这种方式，Kafka 可以高效地管理消息，并且支持快速的磁盘读取。</p>

                <div class="mermaid bg-white p-6 rounded-lg shadow-md">
                    graph LR
                        A[Producer] -->|消息1| B[Partition 1]
                        A -->|消息2| C[Partition 2]
                        B --> D[Segment 1]
                        B --> E[Segment 2]
                        C --> F[Segment 1]
                        C --> G[Segment 2]
                        D -->|顺序写入| H[日志文件1.log]
                        E -->|顺序写入| I[日志文件2.log]
                        style A fill:#9F7AEA,color:white
                        style B fill:#4299E1,color:white
                        style C fill:#38B2AC,color:white
                </div>
            </section>

            <!-- Broker Role -->
            <section class="mb-20">
                <div class="flex items-center mb-8">
                    <h2 class="text-3xl font-bold text-gray-800 highlight inline-block">4. Broker 的角色与负载均衡</h2>
                    <div class="ml-4 w-12 h-12 rounded-full bg-yellow-100 flex items-center justify-center">
                        <i class="fas fa-balance-scale text-yellow-600 text-xl"></i>
                    </div>
                </div>
                <p class="text-gray-700 mb-6">Kafka 的 <strong>Broker</strong> 是 Kafka 集群中的基本单位，每个 Broker 都负责存储一部分 <strong>Partition</strong>。多个 Broker 协同工作，共同为整个 Kafka 集群提供服务。Broker 之间通过 <strong>Zookeeper</strong> 来协调和同步其状态。</p>
                
                <div class="grid md:grid-cols-2 gap-6 mb-8">
                    <div>
                        <h3 class="text-xl font-semibold mb-4 text-gray-800">负载均衡</h3>
                        <p class="text-gray-700 mb-4">Kafka 的负载均衡是基于 <strong>Partition</strong> 的。当有新的消息生产者连接时，Kafka 会根据负载和分区数选择一个合适的 Broker 来存储消息。负载均衡的核心是在多个 Broker 之间均匀分布消息分区，以确保集群的资源得到最优利用。</p>
                        
                        <div class="bg-gray-800 text-gray-100 p-4 rounded-lg code-block mb-4">
                            <div class="flex items-center text-sm mb-4 text-gray-400">
                                <div class="w-3 h-3 rounded-full bg-red-500 mr-2"></div>
                                <div class="w-3 h-3 rounded-full bg-yellow-500 mr-2"></div>
                                <div class="w-3 h-3 rounded-full bg-green-500 mr-2"></div>
                                <span class="ml-2">LoadBalancer.java</span>
                            </div>
                            <pre><code class="language-java">// 选择合适的 Broker 存储消息
public Broker chooseBrokerForPartition(TopicPartition partition) {
    List<Broker> brokers = getBrokersForTopic(partition.getTopic());
    // 基于负载选择合适的 Broker
    return brokers.stream().min(Comparator.comparingInt(Broker::getLoad)).orElseThrow();
}</code></pre>
                        </div>
                    </div>
                    <div>
                        <div class="bg-white p-6 rounded-lg shadow-md border border-gray-200 h-full">
                            <div class="flex items-center mb-4">
                                <i class="fas fa-chart-pie text-blue-500 mr-2"></i>
                                <h3 class="text-lg font-semibold">Broker关键指标</h3>
                            </div>
                            <div class="space-y-4">
                                <div>
                                    <div class="flex justify-between text-sm text-gray-600 mb-1">
                                        <span>CPU使用率</span>
                                        <span>35%</span>
                                    </div>
                                    <div class="w-full bg-gray-200 rounded-full h-2">
                                        <div class="bg-green-500 h-2 rounded-full" style="width: 35%"></div>
                                    </div>
                                </div>
                                <div>
                                    <div class="flex justify-between text-sm text-gray-600 mb-1">
                                        <span>内存使用</span>
                                        <span>48%</span>
                                    </div>
                                    <div class="w-full bg-gray-200 rounded-full h-2">
                                        <div class="bg-blue-500 h-2 rounded-full" style="width: 48%"></div>
                                    </div>
                                </div>
                                <div>
                                    <div class="flex justify-between text-sm text-gray-600 mb-1">
                                        <span>磁盘IO</span>
                                        <span>25%</span>
                                    </div>
                                    <div class="w-full bg-gray-200 rounded-full h-2">
                                        <div class="bg-purple-500 h-2 rounded-full" style="width: 25%"></div>
                                    </div>
                                </div>
                                <div>
                                    <div class="flex justify-between text-sm text-gray-600 mb-1">
                                        <span>网络吞吐</span>
                                        <span>60%</span>
                                    </div>
                                    <div class="w-full bg-gray-200 rounded-full h-2">
                                        <div class="bg-yellow-500 h-2 rounded-full" style="width: 60%"></div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </section>

            <!-- Replication -->
            <section class="mb-20">
                <div class="flex items-center mb-8">
                    <h2 class="text-3xl font-bold text-gray-800 highlight inline-block">5. 副本机制与数据一致性</h2>
                    <div class="ml-4 w-12 h-12 rounded-full bg-red-100 flex items-center justify-center">
                        <i class="fas fa-copy text-red-600 text-xl"></i>
                    </div>
                </div>
                <p class="text-gray-700 mb-6">Kafka 使用 <strong>副本机制</strong> 来保证消息的高可用性和一致性。每个 Partition 都有多个副本，这些副本分别存储在不同的 Broker 上。Kafka 使用 <strong>Leader</strong> 和 <strong>Follower</strong> 来管理副本：每个 Partition 只有一个 Leader，其他副本为 Follower。</p>
                
                <div class="bg-white p-6 rounded-lg shadow-md border border-gray-200 mb-8">
                    <div class="flex items-center mb-4">
                        <i class="fas fa-sync-alt text-blue-500 mr-2"></i>
                        <h3 class="text-lg font-semibold">数据同步与一致性</h3>
                    </div>
                    <p class="text-gray-700 mb-4">Kafka 通过 <strong>ISR</strong>（In-Sync Replica）机制来保证数据的一致性。每当 <strong>Leader</strong> 接收到写入请求后，它会将数据同步到所有的 Follower 中，只有当所有 Follower 都成功写入后，Leader 才会确认写操作完成。</p>
                    
                    <div class="bg-gray-800 text-gray-100 p-4 rounded-lg code-block">
                        <div class="flex items-center text-sm mb-4 text-gray-400">
                            <div class="w-3 h-3 rounded-full bg-red-500 mr-2"></div>
                            <div class="w-3 h-3 rounded-full bg-yellow-500 mr-2"></div>
                            <div class="w-3 h-3 rounded-full bg-green-500 mr-2"></div>
                            <span class="ml-2">ReplicationManager.java</span>
                        </div>
                        <pre><code class="language-java">// 写操作同步到所有副本
public void replicateMessageToFollowers(Partition partition) {
    for (Replica replica : partition.getFollowers()) {
        replica.sync();
    }
}</code></pre>
                    </div>
                </div>

                <div class="mermaid bg-white p-6 rounded-lg shadow-md">
                    graph TB
                        subgraph Broker 1
                            A[Leader Partition 0] -->|同步数据| B[Follower Partition 0]
                        end
                        subgraph Broker 2
                            C[Follower Partition 0] -->|同步确认| A
                            D[Leader Partition 1] -->|同步数据| C
                        end
                        subgraph Broker 3
                            B -->|同步确认| D
                            E[Follower Partition 1] -->|同步确认| D
                        end
                        style A fill:#38B2AC,color:white
                        style B fill:#4299E1,color:white
                        style C fill:#4299E1,color:white
                        style D fill:#38B2AC,color:white
                        style E fill:#4299E1,color:white
                </div>
            </section>

            <!-- Message Consumption -->
            <section class="mb-20">
                <div class="flex items-center mb-8">
                    <h2 class="text-3xl font-bold text-gray-800 highlight inline-block">6. 消息消费与偏移量管理</h2>
                    <div class="ml-4 w-12 h-12 rounded-full bg-indigo-100 flex items-center justify-center">
                        <i class="fas fa-exchange-alt text-indigo-600 text-xl"></i>
                    </div>
                </div>
                <p class="text-gray-700 mb-6">Kafka 消费者从 Kafka 中消费消息时，消息的读取是通过 <strong>Offset</strong> 来进行管理的。消费者在消费时，可以选择提交或不提交 <strong>Offset</strong>。Kafka 提供了两种方式来管理消费者的 <strong>Offset</strong>：</p>
                
                <div class="grid md:grid-cols-2 gap-6 mb-8">
                    <div class="bg-white p-6 rounded-lg shadow-md border-t-4 border-blue-500">
                        <h3 class="text-xl font-semibold mb-4 text-gray-800">自动提交</h3>
                        <p class="text-gray-700 mb-4">消费者自动提交消息的偏移量。</p>
                        <div class="bg-gray-800 text-gray-100 p-4 rounded-lg code-block">
                            <div class="flex items-center text-sm mb-4 text-gray-400">
                                <div class="w-3 h-3 rounded-full bg-red-500 mr-2"></div>
                                <div class="w-3 h-3 rounded-full bg-yellow-500 mr-2"></div>
                                <div class="w-3 h-3 rounded-full bg-green-500 mr-2"></div>
                                <span class="ml-2">Consumer.java</span>
                            </div>
                            <pre><code class="language-java">// 自动提交偏移量
props.put("enable.auto.commit", "true");
props.put("auto.commit.interval.ms", "1000");</code></pre>
                        </div>
                    </div>
                    <div class="bg-white p-6 rounded-lg shadow-md border-t-4 border-purple-500">
                        <h3 class="text-xl font-semibold mb-4 text-gray-800">手动提交</h3>
                        <p class="text-gray-700 mb-4">消费者在处理完消息后，手动提交偏移量。</p>
                        <div class="bg-gray-800 text-gray-100 p-4 rounded-lg code-block">
                            <div class="flex items-center text-sm mb-4 text-gray-400">
                                <div class="w-3 h-3 rounded-full bg-red-500 mr-2"></div>
                                <div class="w-3 h-3 rounded-full bg-yellow-500 mr-2"></div>
                                <div class="w-3 h-3 rounded-full bg-green-500 mr-2"></div>
                                <span class="ml-2">Consumer.java</span>
                            </div>
                            <pre><code class="language-java">// 手动提交偏移量
consumer.commitSync(new OffsetCommitRequest(
    new Offset(record.offset(), record.partition())
));</code></pre>
                        </div>
                    </div>
                </div>

                <div class="bg-indigo-50 p-6 rounded-lg border-l-4 border-indigo-500 mb-8">
                    <p class="text-indigo-800"><i class="fas fa-users text-indigo-500 mr-2"></i> Kafka 通过 <strong>Consumer Group</strong> 机制来支持多消费者的并行消费，每个消费者从 Kafka 中获取自己需要的分区。</p>
                </div>

                <div class="mermaid bg-white p-6 rounded-lg shadow-md">
                    graph LR
                        subgraph Topic A
                            A[Partition 0] --> C[Consumer 1]
                            B[Partition 1] --> D[Consumer 2]
                            E[Partition 2] --> F[Consumer 3]
                        end
                        G[Consumer Group] --> C
                        G --> D
                        G --> F
                        style A fill:#4299E1,color:white
                        style B fill:#4299E1,color:white
                        style E fill:#4299E1,color:white
                        style C fill:#9F7AEA,color:white
                        style D fill:#9F7AEA,color:white
                        style F fill:#9F7AEA,color:white
                        style G fill:#667EEA,color:white
                </div>
            </section>

            <!-- Message Delivery -->
            <section class="mb-20">
                <div class="flex items-center mb-8">
                    <h2 class="text-3xl font-bold text-gray-800 highlight inline-block">7. Kafka 的消息传递模型：异步与同步</h2>
                    <div class="ml-4 w-12 h-12 rounded-full bg-pink-100 flex items-center justify-center">
                        <i class="fas fa-paper-plane text-pink-600 text-xl"></i>
                    </div>
                </div>
                <p class="text-gray-700 mb-6">Kafka 的消息传递支持异步和同步两种方式。默认情况下，Kafka 的生产者会使用异步方式发送消息，这样能够提高吞吐量。生产者发送消息时，会向 <strong>Leader Broker</strong> 发送请求，Leader Broker 接收到消息后，会将消息追加到日志文件中，并异步同步到 Follower。</p>
                
                <div class="bg-white p-6 rounded-lg shadow-md border border-gray-200 mb-8">
                    <h3 class="text-xl font-semibold mb-4 text-gray-800">异步发送消息</h3>
                    <p class="text-gray-700 mb-4">Kafka 提供了异步发送机制来提高消息传递的效率。消息发送后，生产者并不等待消息的确认，而是立即返回。</p>
                    
                    <div class="bg-gray-800 text-gray-100 p-4 rounded-lg code-block">
                        <div class="flex items-center text-sm mb-4 text-gray-400">
                            <div class="w-3 h-3 rounded-full bg-red-500 mr-2"></div>
                            <div class="w-3 h-3 rounded-full bg-yellow-500 mr-2"></div>
                            <div class="w-3 h-3 rounded-full bg-green-500 mr-2"></div>
                            <span class="ml-2">Producer.java</span>
                        </div>
                        <pre><code class="language-java">// 异步发送消息，返回一个回调
producer.send(record, new Callback() {
    @Override
    public void onCompletion(RecordMetadata metadata, Exception exception) {
        // 处理回调结果
    }
});</code></pre>
                    </div>
                </div>

                <div class="grid md:grid-cols-2 gap-6">
                    <div class="bg-blue-50 p-6 rounded-lg border-l-4 border-blue-500">
                        <h3 class="text-lg font-semibold mb-3 text-blue-800">异步发送优势</h3>
                        <ul class="space-y-2 text-blue-800">
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-blue-500 mr-2 mt-1"></i>
                                <span>更高的吞吐量</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-blue-500 mr-2 mt-1"></i>
                                <span>更低的延迟</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-blue-500 mr-2 mt-1"></i>
                                <span>更好的资源利用率</span>
                            </li>
                        </ul>
                    </div>
                    <div class="bg-purple-50 p-6 rounded-lg border-l-4 border-purple-500">
                        <h3 class="text-lg font-semibold mb-3 text-purple-800">同步发送优势</h3>
                        <ul class="space-y-2 text-purple-800">
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-purple-500 mr-2 mt-1"></i>
                                <span>更强的数据一致性保证</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-purple-500 mr-2 mt-1"></i>
                                <span>即时错误检测</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-purple-500 mr-2 mt-1"></i>
                                <span>更简单的错误处理</span>
                            </li>
                        </ul>
                    </div>
                </div>
            </section>

            <!-- High Availability -->
            <section class="mb-20">
                <div class="flex items-center mb-8">
                    <h2 class="text-3xl font-bold text-gray-800 highlight inline-block">8. 高可用性与故障恢复</h2>
                    <div class="ml-4 w-12 h-12 rounded-full bg-teal-100 flex items-center justify-center">
                        <i class="fas fa-shield-alt text-teal-600 text-xl"></i>
                    </div>
                </div>
                <p class="text-gray-700 mb-6">Kafka 的高可用性由其 <strong>副本机制</strong> 和 <strong>自动故障恢复</strong> 功能保证。Kafka 在启动时，会自动检查副本的状态，并根据 <strong>Zookeeper</strong> 中的元数据来恢复故障的副本。如果某个 Broker 宕机，Kafka 会自动将该 Broker 上的 <strong>Leader</strong> 副本转移到其他可用的 Broker 上。</p>
                
                <div class="bg-gray-800 text-gray-100 p-4 rounded-lg code-block mb-8">
                    <div class="flex items-center text-sm mb-4 text-gray-400">
                        <div class="w-3 h-3 rounded-full bg-red-500 mr-2"></div>
                        <div class="w-3 h-3 rounded-full bg-yellow-500 mr-2"></div>
                        <div class="w-3 h-3 rounded-full bg-green-500 mr-2"></div>
                        <span class="ml-2">FailoverManager.java</span>
                    </div>
                    <pre><code class="language-java">// 自动将 Leader 副本迁移到新的 Broker
public void handleBrokerFailure(Broker failedBroker) {
    List<Partition> affectedPartitions = getPartitionsOnBroker(failedBroker);
    for (Partition partition : affectedPartitions) {
        // 选择一个新的 Broker 作为 Leader
        Partition newLeader = chooseNewLeader(partition);
        partition.setLeader(newLeader);
    }
}</code></pre>
                </div>

                <div class="mermaid bg-white p-6 rounded-lg shadow-md">
                    sequenceDiagram
                        participant P as Producer
                        participant B1 as Broker1(Leader)
                        participant B2 as Broker2(Follower)
                        participant B3 as Broker3(Follower)
                        participant ZK as Zookeeper
                        
                        P->>B1: 发送消息
                        B1->>B2: 同步数据
                        B1->>B3: 同步数据
                        B2-->>B1: 确认同步
                        B3-->>B1: 确认同步
                        B1-->>P: 确认写入
                        Note over B1: Broker1宕机
                        B2->>ZK: 检测Leader失效
                        ZK->>B2: 选举新Leader
                        B2->>B3: 成为新Leader并同步数据
                        P->>B2: 后续消息发送到新Leader
                </div>
            </section>

            <!-- Scalability -->
            <section class="mb-20">
                <div class="flex items-center mb-8">
                    <h2 class="text-3xl font-bold text-gray-800 highlight inline-block">9. Kafka 集群的扩展性设计</h2>
                    <div class="ml-4 w-12 h-12 rounded-full bg-orange-100 flex items-center justify-center">
                        <i class="fas fa-expand-arrows-alt text-orange-600 text-xl"></i>
                    </div>
                </div>
                <p class="text-gray-700 mb-6">Kafka 的设计允许通过增加 <strong>Broker</strong> 和 <strong>Partition</strong> 来扩展集群的处理能力。Kafka 可以在运行时动态增加新的 Broker，数据会根据 <strong>Partition</strong> 数量和负载情况自动重新分配。</p>
                
                <div class="bg-white p-6 rounded-lg shadow-md border border-gray-200 mb-8">
                    <h3 class="text-xl font-semibold mb-4 text-gray-800">动态扩展</h3>
                    <p class="text-gray-700 mb-4">Kafka 的动态扩展性通过 <strong>Zookeeper</strong> 协调各个 Broker 的信息，并使用 <strong>Partition Rebalancing</strong> 来实现。在扩展过程中，Kafka 会自动将分区迁移到新的 Broker 上，以实现负载均衡。</p>
                    
                    <div class="bg-gray-800 text-gray-100 p-4 rounded-lg code-block">
                        <div class="flex items-center text-sm mb-4 text-gray-400">
                            <div class="w-3 h-3 rounded-full bg-red-500 mr-2"></div>
                            <div class="w-3 h-3 rounded-full bg-yellow-500 mr-2"></div>
                            <div class="w-3 h-3 rounded-full bg-green-500 mr-2"></div>
                            <span class="ml-2">ClusterManager.java</span>
                        </div>
                        <pre><code class="language-java">// 动态添加新的 Broker 并重新分配 Partition
public void addBrokerAndRebalance(Cluster cluster) {
    // 重新分配 Partition
    rebalancePartitions(cluster);
}</code></pre>
                    </div>
                </div>

                <div class="grid md:grid-cols-2 gap-6">
                    <div class="bg-white p-6 rounded-lg shadow-md border border-gray-200">
                        <h3 class="text-xl font-semibold mb-4 text-gray-800">水平扩展</h3>
                        <ul class="space-y-3 text-gray-700">
                            <li class="flex items-start">
                                <div class="flex-shrink-0 h-6 w-6 rounded-full bg-orange-100 flex items-center justify-center mr-3">
                                    <i class="fas fa-plus text-orange-500 text-xs"></i>
                                </div>
                                <span>增加Broker节点提升整体吞吐量</span>
                            </li>
                            <li class="flex items-start">
                                <div class="flex-shrink-0 h-6 w-6 rounded-full bg-orange-100 flex items-center justify-center mr-3">
                                    <i class="fas fa-columns text-orange-500 text-xs"></i>
                                </div>
                                <span>增加Partition数量提高并行处理能力</span>
                            </li>
                            <li class="flex items-start">
                                <div class="flex-shrink-0 h-6 w-6 rounded-full bg-orange-100 flex items-center justify-center mr-3">
                                    <i class="fas fa-random text-orange-500 text-xs"></i>
                                </div>
                                <span>自动重新分配Partition实现负载均衡</span>
                            </li>
                        </ul>
                    </div>
                    <div class="bg-white p-6 rounded-lg shadow-md border border-gray-200">
                        <h3 class="text-xl font-semibold mb-4 text-gray-800">垂直扩展</h3>
                        <ul class="space-y-3 text-gray-700">
                            <li class="flex items-start">
                                <div class="flex-shrink-0 h-6 w-6 rounded-full bg-orange-100 flex items-center justify-center mr-3">
                                    <i class="fas fa-memory text-orange-500 text-xs"></i>
                                </div>
                                <span>提升单节点硬件配置(CPU/内存/磁盘)</span>
                            </li>
                            <li class="flex items-start">
                                <div class="flex-shrink-0 h-6 w-6 rounded-full bg-orange-100 flex items-center justify-center mr-3">
                                    <i class="fas fa-hdd text-orange-500 text-xs"></i>
                                </div>
                                <span>使用高性能存储(SSD/NVMe)</span>
                            </li>
                            <li class="flex items-start">
                                <div class="flex-shrink-0 h-6 w-6 rounded-full bg-orange-100 flex items-center justify-center mr-3">
                                    <i class="fas fa-network-wired text-orange-500 text-xs"></i>
                                </div>
                                <span>优化网络配置(10GbE/25GbE)</span>
                            </li>
                        </ul>
                    </div>
                </div>
            </section>

            <!-- Summary -->
            <section class="mb-20 bg-gradient-to-r from-blue-50 to-purple-50 p-8 rounded-xl">
                <div class="flex items-center mb-6">
                    <h2 class="text-3xl font-bold text-gray-800 highlight inline-block">核心要点总结</h2>
                    <div class="ml-4 w-12 h-12 rounded-full bg-gradient-to-r from-blue-500 to-purple-500 flex items-center justify-center text-white">
                        <i class="fas fa-star text-xl"></i>
                    </div>
                </div>
                
                <div class="grid md:grid-cols-2 gap-6">
                    <div class="bg-white p-6 rounded-lg shadow-md">
                        <ul class="space-y-4">
                            <li class="flex items-start">
                                <div class="flex-shrink-0 w-8 h-8 rounded-full bg-blue-100 flex items-center justify-center mr-4">
                                    <span class="text-blue-600 font-bold">1</span>
                                </div>
                                <div>
                                    <h3 class="font-semibold text-gray-800">分布式日志架构</h3>
                                    <p class="text-gray-600 text-sm">基于Partition的水平扩展能力</p>
                                </div>
                            </li>
                            <li class="flex items-start">
                                <div class="flex-shrink-0 w-8 h-8 rounded-full bg-blue-100 flex items-center justify-center mr-4">
                                    <span class="text-blue-600 font-bold">2</span>
                                </div>
                                <div>
                                    <h3 class="font-semibold text-gray-800">高吞吐量设计</h3>
                                    <p class="text-gray-600 text-sm">顺序I/O、批量处理、零拷贝等技术</p>
                                </div>
                            </li>
                            <li class="flex items-start">
                                <div class="flex-shrink-0 w-8 h-8 rounded-full bg-blue-100 flex items-center justify-center mr-4">
                                    <span class="text-blue-600 font-bold">3</span>
                                </div>
                                <div>
                                    <h3 class="font-semibold text-gray-800">高可用性保障</h3>
                                    <p class="text-gray-600 text-sm">副本机制与自动故障转移</p>
                                </div>
                            </li>
                        </ul>
                    </div>
                    <div class="bg-white p-6 rounded-lg shadow-md">
                        <ul class="space-y-4">
                            <li class="flex items-start">
                                <div class="flex-shrink-0 w-8 h-8 rounded-full bg-purple-100 flex items-center justify-center mr-4">
                                    <span class="text-purple-600 font-bold">4</span>
                                </div>
                                <div>
                                    <h3 class="font-semibold text-gray-800">灵活的消息模型</h3>
                                    <p class="text-gray-600 text-sm">支持发布/订阅和队列模式</p>
                                </div>
                            </li>
                            <li class="flex items-start">
                                <div class="flex-shrink-0 w-8 h-8 rounded-full bg-purple-100 flex items-center justify-center mr-4">
                                    <span class="text-purple-600 font-bold">5</span>
                                </div>
                                <div>
                                    <h3 class="font-semibold text-gray-800">水平扩展能力</h3>
                                    <p class="text-gray-600 text-sm">动态添加Broker和Partition</p>
                                </div>
                            </li>
                            <li class="flex items-start">
                                <div class="flex-shrink-0 w-8 h-8 rounded-full bg-purple-100 flex items-center justify-center mr-4">
                                    <span class="text-purple-600 font-bold">6</span>
                                </div>
                                <div>
                                    <h3 class="font-semibold text-gray-800">持久化与实时性</h3>
                                    <p class="text-gray-600 text-sm">消息持久化与低延迟消费</p>
                                </div>
                            </li>
                        </ul>
                    </div>
                </div>
            </section>
        </div>
    </main>

    <!-- Footer -->
    <footer class="bg-gray-900 text-gray-300 py-12">
        <div class="container mx-auto px-6">
            <div class="max-w-4xl mx-auto">
                <div class="flex flex-col items-center text-center">
                    <h2 class="text-2xl font-bold text-white mb-4">技术小馆</h2>
                    <p class="mb-6 max-w-md">深入浅出解析核心技术，助力开发者成长</p>
                    <div class="mb-8">
                        <a href="http://www.yuque.com/jtostring" class="inline-block bg-blue-600 hover:bg-blue-700 text-white font-medium py-3 px-6 rounded-lg transition duration-300 transform hover:-translate-y-1">
                            访问语雀主页 <i class="fas fa-external-link-alt ml-2"></i>
                        </a>
                    </div>
                    <div class="border-t border-gray-800 w-full pt-8">
                        <p class="text-sm text-gray-500">© 2023 技术小馆. 保留所有权利.</p>
                    </div>
                </div>
            </div>
        </div>
    </footer>

    <script>
        mermaid.initialize({
            startOnLoad: true,
            theme: 'default',
            flowchart: {
                useMaxWidth: true,
                htmlLabels: true,
                curve: 'basis'
            }
        });
        
        // Smooth scrolling for anchor links
        document.querySelectorAll('a[href^="#"]').forEach(anchor => {
            anchor.addEventListener('click', function (e) {
                e.preventDefault();
                document.querySelector(this.getAttribute('href')).scrollIntoView({
                    behavior: 'smooth'
                });
            });
        });
        
        // Card hover effect
        const cards = document.querySelectorAll('.card-hover');
        cards.forEach(card => {
            card.addEventListener('mouseenter', () => {
                card.style.transition = 'all 0.3s ease';
            });
        });
    </script>
</body>
</html>
```